﻿using System;
using WDA.Core;
using System.Web;
using System.Web.UI;

namespace WDA.Web
{
	/// <summary>
	/// WDAB facade specially crafted to use in web environment. Includes extension methods of <see cref="Page"/>
	/// class for obtaining WDAB service proxies.
	/// </summary>
	public static class WDAServices
	{
		/// <summary>
		/// Returns proxy for service of given type. Internally uses <see cref="Services"/> instance contained
		/// in request-specific data.		
		/// </summary>
		/// <param name="serviceType">CLR type of service contract.</param>
		/// <returns>A proxy object.</returns>
		public static object Get(Type serviceType)
		{
			Services services = (Services)HttpContext.Current.Items[WDAHttpModule.ServicesInstanceKey];
			return services.Get(serviceType);
		}
		/// <summary>
      /// Returns proxy for service of given type. Internally uses <see cref="Services"/> instance contained
      /// in request-specific data.		
		/// </summary>
		/// <param name="page">ASP.NET Page.</param>
      /// <param name="serviceType">CLR type of service contract.</param>
      /// <returns>A proxy object.</returns>
		public static object GetService(this Page page, Type serviceType)
		{
			return Get(serviceType);
		}
		/// <summary>
      /// Returns proxy for service of given type. Internally uses <see cref="Services"/> instance contained
      /// in request-specific data.	
		/// </summary>
      /// <typeparam name="T">CLR type of service contract.</typeparam>
      /// <returns>A proxy object.</returns>
		public static T Get<T>()
		{
			Services services = (Services)HttpContext.Current.Items[WDAHttpModule.ServicesInstanceKey];
			return services.Get<T>();
		}
		/// <summary>
      /// Returns proxy for service of given type. Internally uses <see cref="Services"/> instance contained
      /// in request-specific data.	
		/// </summary>
      /// <typeparam name="T">CLR type of service contract.</typeparam>
      /// <param name="page">ASP.NET Page.</param>
      /// <returns>A proxy object.</returns>
		public static T GetService<T>(this Page page)
		{
			return Get<T>();
		}
	}
}
